package com.kimo3412.bus_line.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kimo3412.bus_line.entity.BusLine;
import com.kimo3412.bus_line.service.BusLineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/busLine")
public class BusLineController {

    @Autowired
    private BusLineService busLineService;

    // 查询所有线路
    @GetMapping("/list")
    public List<BusLine> list() {
        return busLineService.list();
    }

    // 根据名称查询线路
    @GetMapping("/searchByName")
    public BusLine searchByName(@RequestParam String lineName) {
        return busLineService.getOne(new QueryWrapper<BusLine>().eq("line_name", lineName));
    }

    // 根据线路号查询线路
    @GetMapping("/searchByNumber")
    public BusLine searchByNumber(@RequestParam String lineNumber) {
        return busLineService.getOne(new QueryWrapper<BusLine>().eq("line_number", lineNumber));
    }

    // 添加线路
    @PostMapping("/add")
    public boolean add(@RequestBody BusLine busLine) {
        return busLineService.save(busLine);
    }

    // 修改指定线路
    @PostMapping("/update/{id}")  // 使用路径参数指定线路 ID
    public boolean update(@PathVariable Long id, @RequestBody BusLine busLine) {
        // 设置要更新的线路 ID
        busLine.setId(id);
        return busLineService.updateById(busLine);
    }

    // 删除线路
    @DeleteMapping("/delete/{id}")
    public boolean delete(@PathVariable Long id) {
        return busLineService.removeById(id);
    }
}
